package com.csdj.t309.geamtest.service;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Hashs implements Ihash{
    @Override
    public boolean yanzheng(String username, String email, String nickname, String desc, String id, String hash,String bbhash) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String pai = paixv(username, email, nickname, desc);
        String aa = pai+id+hash;
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(aa.getBytes("UTF-8"));
        byte[] byhash1 = messageDigest.digest();
        String aahash = byteToString(byhash1);
        if(aahash.equals(bbhash)){
            return true;
        }
        return false;
    }

    public static String byteToString(byte[] bytes){
        StringBuffer stringBuffer = new StringBuffer();
        for(Integer i = 0 ; i < bytes.length ; i++){
            if(Integer.toHexString(0xFF & bytes[i]).length()==1){
                stringBuffer.append("0").append(Integer.toHexString(0xFF & bytes[i]));
            }else {
                stringBuffer.append(Integer.toHexString(0xFF & bytes[i]));
            }
        }
        return stringBuffer.toString().toUpperCase();
    }

    public static String paixv(String username, String email, String nickname, String desc){
        List<String> list = new  ArrayList<>();
        list.add(email);
        list.add(nickname);
        list.add(desc);
        Collections.sort(list);
        String paixv = username;
        for(String aa : list){
            paixv=paixv+aa;
        }
        return paixv;
    }

    public static void main(String[] args) {
        System.out.println(paixv("山丹","2379323670","田港","nickname"));
    }
}
